package com.hoshiicloud.user.controller.organization;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.user.entity.organization.PlatOrganizationUsers;
import com.hoshiicloud.user.param.organization.PlatOrganizationUsersAddParam;
import com.hoshiicloud.user.param.organization.PlatOrganizationUsersQueryParam;
import com.hoshiicloud.user.param.organization.PlatOrganizationUsersUpdateParam;
import com.hoshiicloud.user.service.organization.PlatOrganizationUsersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 平台机构用户 控制器
 * </p>
 *
 * @author campchen
 * @since 2019-05-27
 */
@Slf4j
@RestController
@RequestMapping("/organization/plat-organization-users")
@Api(value = "租户平台机构用户", tags = {"租户平台机构用户"})
@ApiSort(29)
public class PlatOrganizationUsersController {
    @Autowired
    private PlatOrganizationUsersService platOrganizationUsersService;

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 分页查询平台机构用户
     */
    @ApiOperation(value = "分页查询平台机构用户")
    @PostMapping("/getPage")
    public Response page(@RequestBody PlatOrganizationUsersQueryParam param) {
        try {
            QueryWrapper<PlatOrganizationUsers> queryWrapper = new QueryWrapper<PlatOrganizationUsers>();
            queryWrapper.lambda()
                    .eq((param.getMemberId() != null), PlatOrganizationUsers::getMemberId, param.getMemberId())
                    .eq(StringUtils.isNotBlank(param.getUserCode()), PlatOrganizationUsers::getUserCode, param.getUserCode())
                    .eq(StringUtils.isNotBlank(param.getQrCode()), PlatOrganizationUsers::getQrCode, param.getQrCode())
                    .eq(StringUtils.isNotBlank(param.getWorkTelphone()), PlatOrganizationUsers::getWorkTelphone, param.getWorkTelphone())
                    .eq(StringUtils.isNotBlank(param.getPosition()), PlatOrganizationUsers::getPosition, param.getPosition())
                    .eq(StringUtils.isNotBlank(param.getEducational()), PlatOrganizationUsers::getEducational, param.getEducational())
                    .eq(StringUtils.isNotBlank(param.getProfessional()), PlatOrganizationUsers::getProfessional, param.getProfessional())
                    .eq((param.getHireDate() != null), PlatOrganizationUsers::getHireDate, param.getHireDate())
                    .eq(StringUtils.isNotBlank(param.getEmail()), PlatOrganizationUsers::getEmail, param.getEmail())
                    .eq(StringUtils.isNotBlank(param.getOfficeLocation()), PlatOrganizationUsers::getOfficeLocation, param.getOfficeLocation())
                    .eq((param.getLocked() != null), PlatOrganizationUsers::getLocked, param.getLocked())
                    .eq(StringUtils.isNotBlank(param.getWxOpenId()), PlatOrganizationUsers::getWxOpenId, param.getWxOpenId())
                    .eq(StringUtils.isNotBlank(param.getWxUnionId()), PlatOrganizationUsers::getWxUnionId, param.getWxUnionId())
                    .eq(StringUtils.isNotBlank(param.getWxName()), PlatOrganizationUsers::getWxName, param.getWxName())
                    .eq(StringUtils.isNotBlank(param.getWxPic()), PlatOrganizationUsers::getWxPic, param.getWxPic())
                    .eq(StringUtils.isNotBlank(param.getQqOpenId()), PlatOrganizationUsers::getQqOpenId, param.getQqOpenId())
                    .eq((param.getDeleted() != null), PlatOrganizationUsers::getDeleted, param.getDeleted())
                    .eq((param.getLastLoginTime() != null), PlatOrganizationUsers::getLastLoginTime, param.getLastLoginTime());
            Page<PlatOrganizationUsers> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = param.toString() + ",Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
            return Response.data(platOrganizationUsersService.getPage(page, queryWrapper, key));
        } catch (Exception e) {
            log.error("分页查询平台机构用户异常", e);
            return Response.failed("分页查询平台机构用户异常");
        }
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据id查询平台机构用户
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询平台机构用户")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(platOrganizationUsersService.getById(id));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增平台机构用户")
    public Response save(@RequestBody @Valid PlatOrganizationUsersAddParam param) {
        PlatOrganizationUsers data;
        try {
            data = BeanUtils.covertType(param, PlatOrganizationUsers.class);
        } catch (Exception e) {
            log.error("新增平台机构用户Bean转换异常", e);
            return Response.failed("新增平台机构用户Bean转换异常");
        }
        return Response.data(platOrganizationUsersService.insert(data));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 修改平台机构用户
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改平台机构用户")
    public Response update(@RequestBody @Valid PlatOrganizationUsersUpdateParam param) {
        PlatOrganizationUsers data;
        try {
            data = BeanUtils.covertType(param, PlatOrganizationUsers.class);
        } catch (Exception e) {
            log.error("修改平台机构用户Bean转换异常", e);
            return Response.failed("修改平台机构用户Bean转换异常");
        }
        return Response.data(platOrganizationUsersService.updateById(data));
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 删除平台机构用户
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除平台机构用户")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response delete(@PathVariable("id") Long id) {
        return Response.data(platOrganizationUsersService.deleteById(id));

    }

    /**
     * @Author:campchen
     * @Description: StringUtils.join(ids, " ") 为缓存中添加唯一KEY  如不合适可选择更好方法
     * @Date:上午 10:18 2019/5/11
     * @Param: ids
     * @return: com.hoshiicloud.common.rpc.Response
     */
    @ApiOperation(value = "根据id集合查询平台机构用户")
    @GetMapping("/getByIds")
    public Response getByIds(@RequestParam List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Response.failed("查询平台机构用户id集合不能为空");
        }
        ids.sort(null);
        return Response.data(platOrganizationUsersService.getByIds(ids, StringUtils.join(ids, "")));
    }


}
